#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(125,125, 125, 255)) )
    {
        return false;
    }
    
    count = 0;
    // load resource directly
    CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("running_girl.ExportJson");
    
    CCArmature *armature = CCArmature::create("running_girl");
    armature->getAnimation()->playByIndex(0);
    armature->getAnimation()->setAnimationInternal(0.03);
    armature->setPosition(ccp(300,300));
    
    armature->setScaleX(-1);
    
    armature->getAnimation()->setMovementEventCallFunc(this, movementEvent_selector(HelloWorld::animationEvent));
    addChild(armature,1,1);
    
    
    //scheduleUpdate();
    return true;
}


void HelloWorld::delay(float dt){
    CCSprite* s = (CCSprite*)getChildByTag(1);
    s->stopAllActions();
}

void HelloWorld::animationEvent(cocos2d::extension::CCArmature *armature, MovementEventType movementType, const char *movementID)
{
//{
//    std::string id = movementID;
//    
//    if (movementType == LOOP_COMPLETE)
//    {
//        if (id.compare("Fire") == 0)
//        {
//            CCActionInterval *actionToRight = CCMoveTo::create(2, ccp(VisibleRect::right().x - 50, VisibleRect::right().y));
//            armature->stopAllActions();
//            armature->runAction(CCSequence::create(actionToRight,  CCCallFunc::create(this, callfunc_selector(TestAnimationEvent::callback1)), NULL));
//            armature->getAnimation()->play("Walk");
//        }
//        else if (id.compare("FireMax") == 0)
//        {
//            CCActionInterval *actionToLeft = CCMoveTo::create(2, ccp(VisibleRect::left().x + 50, VisibleRect::left().y));
//            armature->stopAllActions();
//            armature->runAction(CCSequence::create(actionToLeft,  CCCallFunc::create(this, callfunc_selector(TestAnimationEvent::callback2)), NULL));
//            armature->getAnimation()->play("Walk");
//        }
//    }
    
    std::string id = movementID;
    if (movementType == LOOP_COMPLETE) {
        
        if (id.compare("run") == 0) {
            CCLOG("run complete");
            
            armature->getAnimation()->stop();
            
        }
        
        
        
        
    }
}
void HelloWorld::update(float dt){
    CCArmature* a =  (CCArmature*)getChildByTag(1);
    a->setPositionX(a->getPositionX() + dt * 200);
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    float x = a->getPositionX() > winSize.width * 0.5 ? a->getPositionX() : winSize.width * 0.5;
    CCLOG("%f",x);
    if(a->getPositionX() > winSize.width * 0.5)
        x = a->getPositionX() < 2000 - winSize.width * 0.5 ? a->getPositionX() : 2000 - winSize.width * 0.5;
    
    
    CCPoint realPoint = ccp(x,winSize.height/4);
    CCPoint cameraPoint = ccp(winSize.width * 0.5,winSize.height/4);
    
    setPosition(ccpSub(cameraPoint, realPoint));
}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}
